<?php
require_once 'base.php';
class helper_sms_reg extends helper_sms_base
{
	private $_lock_num = 1;		//错误次数 即锁定
	private $_timeout = 60000; //验证码多少秒内不能重复发送
	private $_result_key = '01#'; //成功状态标志  发送成功
	private $_rndkey = ''; //随机码
	static $Session_key = 'sms_mobileinfo';
	static $dealy_session_key = 'sms_lasttime';
	function set_lock_num($v)
	{
		$this->_lock_num = intval($v);
	}
	function set_timeout($v)
	{
		$this->_timeout = intval($v);
	}
	function _before_send()
	{
		
		if(isset($_SESSION[self::$dealy_session_key]))
		{
		
			$lasttime = $_SESSION[self::$dealy_session_key];
				//重复提交
				if (time() - $lasttime <= $this->_timeout)
				{
					$this->err_msg = intval($this->_timeout / 1000).' 秒内不能重复获取验证码!';
					return false;
				}		
		}else
		{
							//生成验证码
			$this->_rndkey = rand(10000, 99999);
			
			$this->_content = str_replace('{$key}', $this->_rndkey, $this->_content); //替换随即码格式
			
			
			//发送开始前 记录验证码
			$_SESSION[self::$Session_key] = array('rndkey'=>$this->_rndkey,'starttime'=>time()) ;//设置验证码内容
			$_SESSION[self::$dealy_session_key] = time(); //记录发送时间
			
		}
		return true;
	}
	
	function _after_send()
	{
		if(strpos($this->result,$this->_result_key) === false)
		//if ($this->_result_key == $this->result)
		{
			$this->err_msg = '短信接口返回失败';
			return false;	
		}else {
			return true;
		}
	}
	/**
	 * 
	 *2011-12-29-下午09:53:56 by 460932465
	 * 验证输入的验证码是否正确...
	 * @param 提交的值 $value
	 */
	static function validate($value)
	{
		
		if(!is_numeric($value))
		{
			//self::_destroy_rndkey(); //注销状态
			return  false;
		}else if(isset($_SESSION[self::$Session_key]) && is_array($_SESSION[self::$Session_key]))
		{
			$sendinfo = $_SESSION[self::$Session_key];
			$status = $value == $sendinfo['rndkey'];
			if ($status == true){
				self::_destroy_rndkey();
			}
			return $status;
		}else {
			//self::_destroy_rndkey();//注销状态
			return false;
		}
	}
	/**
	 * 
	 *2011-12-31-下午02:22:19 by 460932465
	 * @access 销毁短信验证码
	 */
	static function _destroy_rndkey()
	{
		if(isset($_SESSION[self::$Session_key]))
		{
			unset($_SESSION[self::$Session_key]);
		}
		return true;
	}
}

?>